/**
 * @ClassName: AboutFragment
 * @Description: 关于页面
 * @author Mengting Lu
 * @date 2015年7月31日 下午1:58:35
 */

package com.shanghaionstar.discover.charge;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.discover.charge.adapter.ChargeModeTimeAdapter;
import com.shanghaionstar.discover.charge.iview.IChargeModeTimeView;
import com.shanghaionstar.discover.charge.model.ChargeModeTimeItem;
import com.shanghaionstar.discover.charge.presenter.ChargeModeTimePresenter;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.weight.TopLayout;

import java.util.List;

/**
 * @author Kai Zhang
 * @ClassName: ChargeModeTimeFragment
 * @Description: 充电模式设置时间
 * @date 2015年12月10日
 */

public class ChargeModeTimeFragment extends BaseFragment implements IChargeModeTimeView {
	@FindViewById
	private TopLayout top_layout;
	@FindViewById
	private ListView charge_mode_time_lv;
	@FindViewById
	private ProgressLayout mode_time_progress;

	private ChargeModeTimeAdapter adapter;
	private ChargeModeTimePresenter presenter;

	/**
	 * (non-Javadoc)
	 * <p>
	 * Title: onActivityCreated
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 *
	 * @param savedInstanceState
	 * @see BaseFragment#onActivityCreated(Bundle)
	 */
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if (isFirstLoad()) {
//			top_layout.setLeftButtonClickListener(new OnClickListener() {
//				@Override
//				public void onClick(View v) {
//					back();
//				}
//			});
//			top_layout.setRightClickListener(R.string.btn_save, R.drawable.onstar_personal_btn,
//					new OnClickListener() {
//						@Override
//						public void onClick(View arg0) {
//							presenter.onSave();
//						}
//					});
//			top_layout.setRightTextBg(R.drawable.onstar_personal_btn);
			top_layout.setOnRightTextClickLstener(getString(R.string.btn_save), new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					presenter.onSave();
				}
			});
			presenter = new ChargeModeTimePresenter(((BaseActivity) getActivity()).getScheduler());
			presenter.setView(this);
			presenter.initList();
			presenter.requestSchedule();
		}
	}

	/**
	 * (non-Javadoc)
	 * <p>
	 * Title: getLayoutId
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 *
	 * @return
	 * @see BaseFragment#getLayoutId()
	 */
	@Override
	public int getLayoutId() {
		return R.layout.discover_charge_mode_time;
	}

	/**
	 * (non-Javadoc)
	 * <p>
	 * Title: onDestroy
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 *
	 * @see android.support.v4.app.Fragment#onDestroy()
	 */

	@Override
	public void onDestroy() {
		super.onDestroy();
		top_layout = null;
		presenter.finish();
	}


	@Override
	public void setAdapter(List<ChargeModeTimeItem> list) {
		adapter = new ChargeModeTimeAdapter(list, getActivity());
		charge_mode_time_lv.setAdapter(adapter);
		adapter.setLv(charge_mode_time_lv);
		charge_mode_time_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				adapter.clicked = true;
				int[] p = presenter.onItemClick(position);
				adapter.retractPosition = p[0];
				adapter.collapsePosition = p[1];
				adapter.doubleAnim = p[0] > -1 && p[1] > -1;
				adapter.notifyDataSetChanged();
			}
		});
	}

	@Override
	public void notifyDataChange() {
		adapter.notifyDataSetChanged();
		adapter.collapsePosition = 0;
	}

	@Override
	public void mBack() {
		back();
	}

	@Override
	public void setLoading(boolean isLoading) {
		mode_time_progress.setVisibility(isLoading?View.VISIBLE:View.GONE);
	}

	@Override
	public void onResponseError(OnStarException e) {
		setLoading(false);
		AlertDialogUtil.showDialogByOnstarException(onActivity(), e);
	}

	@Override
	public void onReponseSuccess(Class<?> object) {

	}

	@Override
	public Activity onActivity() {
		return getActivity();
	}
}
